home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-06-28 | 15.2 KB | 584 lines | [TEXT/PJMM] |
- unit mehitFile;
-
- interface
-
- uses
- Globals, HelloTabby, Centerer;
-
- var
- CLPath, ULPath, SysopName: STR255;
- NextLaunchDateRec: DateTimeRec;
- MsgPath: STR255;
- LowMsg, HiMsg, MSGTXTLength: longint;
- SectionCount: integer;
-
- procedure MakePath (FName: STR255; VRefNum: integer; var MyPath: STR255);
-
- procedure ReadConfig;
-
- procedure ReadMESSAGES;
-
- procedure MakeTextFile (FileName: STR255);
-
- procedure FrameDItem (dLog: DialogPtr; iNum: integer);
-
- function ReadVersion: STR255;
-
- function AtEOF (fRefNum: Integer): Boolean;
-
- function Wr (FileRefNum: integer; TheMessage: string): OSErr;
-
- function WrLn (FileRefNum: integer; TheMessage: string): OSErr;
-
- function ReadLine (FileRefNum: integer; var TheMessage: string): OSErr;
-
- function CopyFile (FromFile, ToFile: str255): OSErr;
-
- function FileExists (Filename: str255): boolean;
-
- implementation
-
- {----------------------------------------------------------------- }
-
- procedure MakePath; {(FName: STR255; VRefNum: integer; var MyPath: STR255)}
-
- var
- MyPB: CInfoPBRec;
-
- begin
- MyPath := '';
- MyPB.ioCompletion := nil;
- MyPB.ioNamePtr := @FName;
- MyPB.ioVRefNum := VRefNum;
- MyPB.ioFDirIndex := 0;
- MyPB.ioDirID := 0;
- Err := PBGetCatInfo(@MyPB, false);
- MyPB.ioFDirIndex := -1;
- MyPB.ioDirID := MyPB.ioDRParID;
- while PBGetCatInfo(@MyPB, false) = NoErr do
- begin
- MyPath := concat(MyPB.ioNamePtr^, ':', MyPath);
- MyPB.ioDirID := MyPB.ioDRParID;
- MyPB.ioFDirIndex := -1;
- end; { while PBGetCatInfo(@MyPB, false) = NoErr }
- end;
-
- {----------------------------------------------------------------- }
-
- function AtEOF;
-
- var
- currPos, eofPos: LongInt;
-
- begin
- Err := GetFPos(fRefNum, currPos);
- Err := GetEOF(fRefNum, eofPos);
- AtEOF := currPos = eofPos
- end;
-
- { ------------------------------------------------------ }
-
- function Wr;
-
- { Writes string (without length byte) to text file, returns error code }
-
- var
- TheLength: longint;
-
- begin
- TheLength := length(TheMessage);
- Wr := FSWrite(FileRefNum, TheLength, Pointer(ord(@TheMessage) + 1));
- end;
-
- {----------------------------------------------------------------- }
-
- function WrLn;
-
- { Writes string (without length byte) to text file, returns error code }
-
- begin
- TheMessage := concat(TheMessage, ENDLINE);
- WrLn := Wr(FileRefNum, TheMessage);
- end;
-
- {----------------------------------------------------------------- }
-
- function ReadLine;
-
- var
- myPB: ParamBlockRec;
- myString: STR255;
-
- begin
- myString := '';
- myPB.ioCompletion := nil;
- myPB.ioRefNum := FileRefNum;
- myPB.ioBuffer := Pointer(@TheMessage[1]);
- myPB.ioReqCount := 255;
- myPB.ioPosMode := 3456; {ASCII 13*256+128}
- myPB.ioPosOffset := 0; {ignored}
- ReadLine := PBRead(@myPB, False);
- TheMessage[0] := char(myPB.ioActCount);
-
- end;
-
- {----------------------------------------------------------------- }
-
- procedure FrameDItem;
-
- var
- iBox: Rect;
- iType: integer;
- iHandle: Handle;
- oldPenState: PenState;
-
- begin
- GetPenState(oldPenState);
- GetDItem(dLog, iNum, iType, iHandle, iBox);
- InsetRect(iBox, -4, -4);
- PenSize(3, 3);
- FrameRoundRect(iBox, 16, 16);
- SetPenState(oldPenState)
- end;
-
- {----------------------------------------------------------------- }
-
- procedure MakeTextFile;
-
- { Procedure sets up QUED-compatible text file }
-
- var
- fndrInfo: FInfo;
-
- begin
- Err := GetFInfo(FileName, vRefNum, fndrInfo);
- if Err = noErr then
- begin
- fndrInfo.fdType := 'TEXT';
- fndrInfo.fdCreator := DefaultsPtr^.TextType;
- Err := SetFInfo(FileName, vRefNum, fndrInfo);
- end
- else
- Err := Create(FileName, vRefNum, 'QED1', 'TEXT');
- end;
-
- {----------------------------------------------------------------- }
-
- procedure MissingFile (WhichOne: STR255);
-
- var
- theDialog: DialogPtr;
- DlogItem: integer;
-
- begin
- theDialog := GetNewDialog(1009, nil, Pointer(-1));
- SetPort(theDialog);
- CenterDLOG(theDialog);
- ShowWindow(theDialog);
- paramtext(WhichOne, '', '', '');
- FrameDItem(theDialog, OK);
- ModalDialog(nil, DlogItem);
- repeat
- until DlogItem = 1;
- DisposDialog(theDialog);
- exitToShell
- end;
-
- {----------------------------------------------------------------- }
-
- procedure ReadMESSAGES;
-
- { Procedure reads the MESSAGES file }
-
- var
- MSCount: integer;
- MsgByte: signedByte;
- MsgString: STR255;
- CharsToSend: longint;
- OneEntry: SectionPtr;
- Counter: byte;
- TestFile: STR255;
- TestRef, MSGRefNum: integer;
-
- begin
- Counter := 0;
- Err := FSOpen(MESSAGESPath, VRefNum, MSGRefNum);
-
- if Err = NoErr then
- begin
- OneEntry := SectionPtr(NewPtr(SizeOf(Section)));
- CharsToSend := 50;
- Err := FSRead(MSGRefNum, CharsToSend, @MsgPath);
- if MsgPath <> '' then
- MsgPath := concat(MsgPath, ':');
-
- CharsToSend := 4;
- Err := SetFPos(MSGRefNum, fsFromStart, 50);
- Err := FSRead(MSGRefNum, CharsToSend, @LowMsg);
- Err := FSRead(MSGRefNum, CharsToSend, @HiMsg);
- Err := FSRead(MSGRefNum, CharsToSend, @MSGTXTLength);
-
- if Err = NoErr then
- for MSCount := 1 to 255 do
- begin
- Err := SetFPos(MSGRefNum, fsFromStart, (62 + (MSCount - 1) * 36));
- CharsToSend := 255;
- Err := FSRead(MSGRefNum, CharsToSend, Ptr(OneEntry));
- if OneEntry^.Name <> '' then
- begin
- Counter := succ(Counter);
- Sections[Counter] := SectionHandle(NewHandle(SizeOf(Section)));
- MoveHHI(Handle(Sections[Counter]));
- HLock(Handle(Sections[Counter]));
- Sections[Counter]^^.Name := OneEntry^.Name;
- Sections[Counter]^^.Number := MSCount;
- end;
- end;
- Err := FSClose(MSGRefNum);
- SectionCount := Counter;
- DisposPtr(Ptr(OneEntry));
-
- TestFile := concat(MsgPath, 'MSGHDR');
- Err := FSOpen(TestFile, VRefNum, TestRef);
- if Err <> NoErr then
- MissingFile('msghdr')
- else
- begin
- Err := FSClose(TestRef);
- TestFile := concat(MsgPath, 'MSGTXT');
- Err := FSOpen(TestFile, VRefNum, TestRef);
- if Err <> NoErr then
- MissingFile('msgtxt')
- else
- Err := FSClose(TestRef);
- end;
- end
- else
- { Error opening MESSAGES }
- MissingFile('messages');
- end;
-
- { ------------------------------------------------------ }
-
- procedure ReadConfig;
-
- { Reads Config file and returns Path:CallerLog, Path:UserLog, Path:MESSAGES, SysopName (all caps) and }
- { NextLaunchDateRec. }
-
- var
- AString: string;
- ALongInt: LongInt;
- ConfigRefNum: integer;
- FileEnd, CharsToSend, NextLaunchTime: longint;
- ConfigErr: OSErr;
- VolName: STR255;
- ConfigErrorFlag: boolean;
- MF: signedbyte;
-
- begin
-
- ConfigErrorFlag := false;
-
- ConfigErr := GetVol(@VolName, VRefNum); { Get volume ref # for default volume }
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- MESSAGESPath := '';
- ULPath := '';
- CLPath := '';
- SysopName := 'SYSTEM OPERATOR';
- ConfigErr := FSOpen(concat(gDefaultpath, 'Config'), VRefNum, ConfigRefNum);
- if (ConfigErr = NoErr) then
- begin
- ConfigErr := GetEOF(ConfigRefNum, FileEnd);
- if (ConfigErr = NoErr) then
- begin
- if (FileEnd > 317) then { Is file longer than our deepest SetFPos (it should be 349)? }
- begin
- CharsToSend := 41;
- ConfigErr := SetFPos(ConfigRefNum, fsFromStart, 57);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- ConfigErr := FSRead(ConfigRefNum, CharsToSend, @AString);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- if length(AString) > 0 then
- ULPath := AString;
- ULPath := concat(ULPath, ':UserLog');
-
- if (ConfigErrorFlag = false) then
- begin
- CharsToSend := 41;
- ConfigErr := SetFPos(ConfigRefNum, fsFromStart, 98);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- ConfigErr := FSRead(ConfigRefNum, CharsToSend, @AString);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- if length(AString) > 0 then
- CLPath := AString;
- CLPath := concat(CLPath, ':CallerLog');
- end;
-
- if (ConfigErrorFlag = false) then
- begin
- CharsToSend := 80;
- ConfigErr := SetFPos(ConfigRefNum, fsFromStart, 139);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- ConfigErr := FSRead(ConfigRefNum, CharsToSend, @AString);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- if length(AString) > 0 then
- MESSAGESPath := AString;
- MESSAGESPath := concat(MESSAGESPath, ':MESSAGES');
- end;
-
- if (ConfigErrorFlag = false) then
- begin
- CharsToSend := 31;
- ConfigErr := SetFPos(ConfigRefNum, fsFromStart, 317);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- ConfigErr := FSRead(ConfigRefNum, CharsToSend, @AString);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- if length(AString) > 0 then
- SysopName := AString
- end;
-
- if (ConfigErrorFlag = false) then
- begin
- CharsToSend := 4;
- ConfigErr := SetFPos(ConfigRefNum, fsFromStart, 308);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- ConfigErr := FSRead(ConfigRefNum, CharsToSend, @ALongInt);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- Secs2Date(ALongInt, NextLaunchDateRec);
- end;
-
- if (ConfigErrorFlag = false) then
- begin
- CharsToSend := 1;
- ConfigErr := SetFPos(ConfigRefNum, fsFromStart, 316);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- ConfigErr := FSRead(ConfigRefNum, CharsToSend, @MF);
- if (ConfigErr <> NoErr) then
- ConfigErrorFlag := true;
- if MF = 0 then
- MultiFinder := false { operating under MultiFinder? }
- else
- MultiFinder := true;
- ConfigErr := FSClose(ConfigRefNum);
- end
-
- end { if FileEnd > 317 }
- else
- ConfigErrorFlag := true;
- end { Error on get eof of Config }
- else
- ConfigErrorFlag := true;
- end { Error on open Config }
- else
- ConfigErrorFlag := true;
- if ConfigErrorFlag then
- begin
- ConfigErr := FSClose(ConfigRefNum);
- MissingFile('config')
- end
- end;
-
- { ------------------------------------------------------ }
-
- function ReadVersion;
-
- type
- NumVersion = packed record
- case INTEGER of
- 0: (
- majorRev: SignedByte; {1st part of version number in BCD}
- MinorAndBugFixRev: SignedByte; {1st and 2nd nibbles in BCD}
- stage: Byte; {stage code: dev, alpha, beta, final}
- nonRelRev: SignedByte
- ); {revision level of non-released version}
-
- 1: (
- version: LONGINT
- ); {to use all 4 fields at one time}
- end;
-
-
- VersRec = record
- numericVersion: NumVersion; {encoded version number}
- countryCode: INTEGER; {country code from intl utilities}
- shortVersion: Str255; {version number string - worst case}
- reserved: Str255; {longMessage string packed after shortVersion }
- end;
-
- VersRecPtr = ^VersRec;
- VersRecHndl = ^VersRecPtr;
-
- const
- dev = $20;
- alpha = $40;
- beta = $60;
- rel = $80;
-
- var
- AString, TheVers: STR255;
- versionHndl: VersRecHndl;
- MinorRev, BugFixRev: integer;
- Final: boolean;
-
- begin
- Final := false;
- versionHndl := VersRecHndl(NewHandle(sizeOf(VersRec)));
- versionHndl := VersRecHndl(GetResource('vers', 1));
-
- with versionHndl^^.numericVersion do
- begin
- if (majorRev > 0) then
- begin
- if majorRev > $0F then
- TheVers := StringOf(majorRev mod $0F : 1)
- else
- TheVers := '';
- majorRev := BitAnd(majorRev, $0F);
- if (majorRev > 0) then
- TheVers := concat(TheVers, StringOf(majorRev : 1));
- TheVers := concat(TheVers, TheVers);
- end { if (majorRev > 0) }
- else
- TheVers := '0';
- end; {with}
-
- NumToString(versionHndl^^.numericVersion.majorRev, TheVers);
- MinorRev := versionHndl^^.numericVersion.MinorAndBugFixRev mod 128;
- BugFixRev := versionHndl^^.numericVersion.MinorAndBugFixRev div 128;
- if (MinorRev > 0) then
- begin
- if MinorRev > $0F then
- AString := StringOf(MinorRev mod $0F : 1)
- else
- AString := '';
- MinorRev := BitAnd(MinorRev, $0F);
- if (MinorRev > 0) then
- AString := concat(AString, StringOf(MinorRev : 1));
- TheVers := concat(TheVers, '.', AString);
- end
- else
- TheVers := concat(TheVers, '.0');
- if (BugFixRev > 0) then
- begin
- if BugFixRev > $0F then
- AString := StringOf(BugFixRev mod $0F : 1)
- else
- AString := '';
- MinorRev := BitAnd(BugFixRev, $0F);
- if (BugFixRev > 0) then
- AString := concat(AString, StringOf(BugFixRev : 1));
- TheVers := concat(TheVers, '.', AString);
- end;
- if (versionHndl^^.numericVersion.stage > 0) then
- begin
- case versionHndl^^.numericVersion.stage of
- dev:
- TheVers := concat(TheVers, 'd');
- alpha:
- TheVers := concat(TheVers, 'a');
- beta:
- TheVers := concat(TheVers, 'ß');
- rel:
- Final := true;
- otherwise
- ;
- end; { Case statement }
- end; { if (versionHndl^^.numericVersion.stage > 0) }
- if (versionHndl^^.numericVersion.stage > 0) & not Final then
- begin
- if versionHndl^^.numericVersion.nonRelRev > 9 then
- begin
- TheVers := concat(TheVers, stringOf(versionHndl^^.numericVersion.nonRelRev div 16 : 1));
- versionHndl^^.numericVersion.nonRelRev := versionHndl^^.numericVersion.nonRelRev mod 16;
- end;
- TheVers := concat(TheVers, StringOf(versionHndl^^.numericVersion.nonRelRev : 1));
- end;
-
- ReadVersion := TheVers;
-
- DisposHandle(Handle(versionHndl));
- end;
-
- { ------------------------------------------------------ }
-
- function FileExists;{(Filename: str255): boolean}
-
- var
- fRef: integer;
-
- begin
- Err := FSOpen(Filename, vRefNum, fRef);
- if Err = NoErr then
- begin
- Err := FSClose(fRef);
- FileExists := true
- end
- else
- FileExists := false
- end;
-
- { ------------------------------------------------------ }
-
- function CopyFile;{(FromFile, ToFile: str255): OSErr}
-
- { Copies all data from one file to another in CopyChunk-size reads & writes. }
- { Sets destination file creator and type to same as origin file. Does not }
- { delete destination file, just overwrites its EOF mark and refills it with }
- { different data. If there's a problem, returns non-zero error code. }
-
- const
- CopyChunk = 1024;
-
- var
- theVol, fromRef, toRef: integer;
- theDir, fileLength: longint;
- fndrInfo: FInfo;
- myHandle: handle;
- howMuch: longint;
-
- begin
- howMuch := CopyChunk;
- Err := HGetFInfo(theVol, theDir, FromFile, fndrInfo);
- if Err = NoErr then
- Err := HOpen(theVol, theDir, FromFile, fsRdPerm, fromRef);
- if Err = NoErr then
- Err := GetEOF(fromRef, fileLength);
- if Err = NoErr then
- Err := HCreate(theVol, theDir, ToFile, fndrInfo.fdCreator, fndrInfo.fdType);
- if Err = NoErr then
- Err := HOpen(theVol, theDir, ToFile, fsRdWrPerm, toRef);
- if Err = NoErr then
- Err := SetEOF(toRef, fileLength); { same as input length }
- if Err = NoErr then
- begin
- myHandle := NewHandle(CopyChunk);
- MoveHHi(myHandle);
- HLock(myHandle);
- while (howMuch = CopyChunk) & (not AtEOF(fromRef)) & (Err = NoErr) do
- begin
- Err := FSRead(fromRef, howMuch, myHandle^);
- Err := FSWrite(toRef, howMuch, myHandle^)
- end;
- HUnLock(myHandle);
- DisposHandle(myHandle);
- myHandle^ := nil;
- Err := NoErr
- end;
- CopyFile := Err;
- Err := FSClose(fromRef);
- Err := FSClose(toRef)
- end;
-
- end. { Unit }